package com.ssm.subject.domain.handler.subejct;

import com.ssm.subject.common.enums.IsDeletedFlagEnum;
import com.ssm.subject.common.enums.SubjectInfoTypeEnum;
import com.ssm.subject.domain.bo.SubjectAnswerBO;
import com.ssm.subject.domain.bo.SubjectInfoBO;
import com.ssm.subject.domain.bo.SubjectOptionBO;
import com.ssm.subject.domain.convert.SubjectJudgeConvert;
import com.ssm.subject.infra.basic.entity.SubjectJudge;
import com.ssm.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 判断策略类
 */
@Component
public class JudgeTypeHandle implements SubejctTypeHandler{
    @Resource
    private SubjectJudgeService subjectJudgeService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入(判断题，列表中只有一条是否正确数据)
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        SubjectJudge subjectJudge = SubjectJudgeConvert.INSTANCE.subjectAnswerBoToJudge(subjectAnswerBO);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectJudge> subjectJudges = subjectJudgeService.query(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOS = SubjectJudgeConvert.INSTANCE.subjectJudgeListToAnswerBoList(subjectJudges);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
